﻿// <copyright file="FrameworkAssemblyInfo.cs" company="Songhay System">
//     Copyright 2008, Bryan D. Wilhite, Songhay System. All rights reserved.
// </copyright>
namespace Songhay
{
    using System;
    using System.Reflection;
    using System.Text;

    /// <summary>
    /// Defines Assembly information.
    /// </summary>
    /// <remarks>
    /// This definition was developed
    /// for About… dialogs in Windows Forms.
    /// </remarks>
    public class FrameworkAssemblyInfo : IFrameworkAssemblyInfo
    {
        /// <summary>
        /// Constructor of this class.
        /// </summary>
        /// <param name="targetAssembly">The target <see cref="System.Reflection.Assembly"/></param>
        public FrameworkAssemblyInfo(Assembly targetAssembly)
        {
            this._dll = targetAssembly;
        }

        /// <summary>
        /// Gets title of assembly.
        /// </summary>
        public string AssemblyTitle
        {
            get
            {
                object[] attributes = this._dll.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (!String.IsNullOrEmpty(titleAttribute.Title))
                    {
                        return titleAttribute.Title;
                    }
                }

                return System.IO.Path.GetFileNameWithoutExtension(this._dll.CodeBase);
            }
        }

        /// <summary>
        /// Gets Assembly version information.
        /// </summary>
        public string AssemblyVersion
        {
            get
            {
#if SILVERLIGHT
                var name = new AssemblyName(this._dll.FullName);
#else
                var name = this._dll.GetName();
#endif
                return name.Version.ToString();
            }
        }

        /// <summary>
        /// Gets detailed Assembly version information.
        /// </summary>
        public string AssemblyVersionDetail
        {
            get
            {
#if SILVERLIGHT
                AssemblyName dllName = new AssemblyName(this._dll.FullName);
#else
                AssemblyName dllName = this._dll.GetName();
#endif
                return String.Format("{0:D}.{1:D2}", dllName.Version.Major, dllName.Version.Minor);
            }
        }

        /// <summary>
        /// Gets Assembly description information.
        /// </summary>
        public string AssemblyDescription
        {
            get
            {
                object[] attributes = this._dll.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return String.Empty;
                }

                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        /// <summary>
        /// Gets Assembly product information.
        /// </summary>
        public string AssemblyProduct
        {
            get
            {
                object[] attributes = this._dll.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return String.Empty;
                }

                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        /// <summary>
        /// Gets Assembly copyright information.
        /// </summary>
        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = this._dll.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return String.Empty;
                }

                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        /// <summary>
        /// Gets Assembly company information.
        /// </summary>
        public string AssemblyCompany
        {
            get
            {
                object[] attributes = this._dll.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return String.Empty;
                }

                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        private Assembly _dll;
    }
}
